home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6233 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on string literals and char arrays
  5. Date: Fri, 23 Feb 1996 07:32:07 GMT
  6. Organization: Netcom
  7. Message-ID: <312d6b0d.113556285@nntp.ix.netcom.com>
  8. References: <KEITHBAR.96Feb22192211@owl.WPI.EDU>
  9. NNTP-Posting-Host: ix-dc9-12.ix.netcom.com
  10. X-NETCOM-Date: Thu Feb 22 11:32:02 PM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. keithbar@owl.WPI.EDU (Keith Barrett) wrote:
  14.  
  15. > This may have been asked before but here it is:
  16. > Could someone explain to me in very great detail what is the
  17. > difference between:
  18. > main()
  19. > {
  20. >   char *s;
  21. >   s = "Keith";
  22. >   printf("%s\n", s);
  23. > }
  24. > and
  25. > main()
  26. > {
  27. >   char s[6];
  28. >   s = "Keith";
  29. >   printf("%s\n", s);
  30. > }
  31. > In the compiler I use (gcc version 2.7.2) the second one will not
  32. > compile and the first does and works. I understand the surface of this
  33. > problem and I am a grad student in computer science so I am looking
  34. > for details.
  35.  
  36. You found the difference -- the former is legal and the latter is not
  37. :-)
  38.  
  39. char *s defines s as a variable containing a pointer to char.  A value
  40. may be assigned to it by assigning a new pointer.
  41.  
  42. char s[6] defines s as an array of char.  Arrays may not be assigned
  43. with the assignment operator.  To store a new value you have to move
  44. it in with something like strcpy().  E.g.,
  45.  
  46.     strcpy(s, "Keith");
  47.  
  48. Note that you can do this if s is a pointer, but only if it has been
  49. suitably initialized.
  50.  
  51. You can initialize an array directly with something like
  52.  
  53.     char s[6] = "Keith";
  54.  
  55. but you can't assign to s after that.
  56.  
  57. > I realize the s in the second example acts as a a const char * and
  58. > theirfore can't be changed. That is why
  59. > char s[6] = "Keith";
  60. > is allowed.
  61.  
  62. No.  s does not act as a const char *.  It acts as an array.
  63.  
  64. > My questions are:
  65. > 1) where exactly is the string literal located. I realize it has
  66. > global scope so I assume it is created right in the data segment.
  67.  
  68. The compiler puts the string literal someplace convenient.  Just where
  69. depends on the compiler.  It may be put in read only memory, do don't
  70. try to change the literal.
  71.  
  72. Note that in
  73.  
  74.     char s[6] = "Keith";
  75.  
  76. the string literal need not actually be stored.  In many cases the
  77. compiler will simply put the correct valued directly in the array.
  78.  
  79. > 2) what does it evaluate to. I think is is a static const char * const
  80. > (a static constant char pointer to a constant char).
  81. >    "Keith"[2] evaluates to a const char right
  82.  
  83. No.  It evaluates to a char.  Note, however, that you cannot safely
  84. change it.
  85.  
  86. > 3) I know that a pointer variable of type (char *) is created for the
  87. > first example but what about example two. Is s a (const char *)? Some
  88. > books say that s in this case is not even created. Because it is a
  89. > const char pointer and can't change it is never created. The real
  90. > address is just used everywhere s is rather than looking it up.
  91. > Assuming a non optimizing compiler.
  92.  
  93. No.  s is an array.  In many situations it is converted to a char *.
  94. It is not a const char pointer.
  95.  
  96. You seem to be under the common misapprehension that pointers and
  97. arrays are the same thing -- they are not.
  98.  
  99. > Any help would be appreciated,
  100. > Keith Barrett
  101.  
  102.  
  103. Michael M Rubenstein
  104.